import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_oldman_health/api/constant.dart';
import 'package:flutter_oldman_health/api/member_service.dart';
import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'package:flutter_oldman_health/utils/utils.dart';

class FamilyMemberAddPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _FamilyMemberAddPageState();
}

class _FamilyMemberAddPageState extends State<FamilyMemberAddPage> {
  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Constant.backgroundColor,
      child: Column(
        children: [
          _getTitle("添加成员"),
          Container(
              margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
              child: TextField(
                  controller: _controller,
                  maxLines: 1,
                  maxLength: 11,
                  keyboardType: TextInputType.number,
                  inputFormatters: [
                    LengthLimitingTextInputFormatter(11),
                    FilteringTextInputFormatter.digitsOnly
                  ],
                  textAlign: TextAlign.center,
                  autofocus: true,
                  style: TextStyle(
                      fontSize: 19,
                      color: Colors.black87,
                      fontWeight: FontWeight.w300),
                  // obscureText: true,
                  decoration: getInputDecoration("请输入手机号码"))),
          Container(
              margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
              padding: EdgeInsets.all(12),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(55)),
                  border: Border.all(color: Color(0xFF333333), width: 1)),
              child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                  onTap: () async {
                    if (_controller.text == null ||
                        _controller.text.length == 0 ||
                        !Utils.phoneStr(_controller.text)||
                        _controller.text.length > 11) {
                      EasyLoading.showInfo("请正确填写手机号码");
                      return;
                    }
                    var response = await FamilyMemberService.addFamilyRelation(
                        phone: _controller.text);
                    if (null != response && response.statusCode == 200) {
                      ResponseDataModel<String> responseDataModel =
                          ResponseDataModel.fromJson(response.data);
                      if (null != responseDataModel) {
                        EasyLoading.showInfo(responseDataModel.success()
                            ? "成功发送添加成员请求"
                            : responseDataModel.msg);
                        if (responseDataModel.success()) {
                          Navigator.pop(context,true);
                        }
                      }
                    }
                  },
                  child: FractionallySizedBox(
                      widthFactor: 1,
                      child: Text("添加",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontWeight: FontWeight.w300, fontSize: 19))))),
        ],
      ),
    );
  }

  InputDecoration getInputDecoration(String hintText) {
    return InputDecoration(
        fillColor: Colors.red,
        hintText: hintText,
        hintStyle: TextStyle(fontSize:18),
        //InputBorder.none  OutlineInputBorder()
        border: UnderlineInputBorder());
  }

  Widget _getTitle([String title = "添加成员"]) {
    return Container(
        color: Constant.appbarColor,
        padding: EdgeInsets.fromLTRB(16, 46, 16, 16),
        child: FractionallySizedBox(
          widthFactor: 1,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Positioned(
                left: 2,
                top: 5,
                child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Container(
                          child: Icon(Icons.arrow_back_ios),
                          padding: EdgeInsets.fromLTRB(15, 0, 20, 10))),
              ),
              Text(title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 22,
                      color: Colors.black87,
                      fontWeight: FontWeight.normal))
            ],
          ),
        ));
  }
}
